﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace uzing
{
    public class uzCDataBase
    {
        public uzCDataBase()
        {
            rowTable = new Hashtable();
            typeTable = new Hashtable();
            rowNameList = new List<string>();
        }

        Hashtable rowTable;
        Hashtable typeTable;
        List<string> rowNameList;

        public bool AddRow(string RowName, object SampleData)
        {
            bool _result = false;
            if (!rowTable.ContainsKey(RowName))
            {
                List<object> _column = new List<object>();
                rowTable.Add(RowName, _column);
                typeTable.Add(RowName, SampleData.GetType());
                rowNameList.Add(RowName);
                _result = true;
            }
            return _result;
        }

        public bool AddColumn(string RowName, int nColumn, object Data)
        {
            bool _result = false;
            if (rowTable.ContainsKey(RowName))
            {
                Type type = (Type)typeTable[RowName];
                List<object> _column = (List<object>)rowTable[RowName];
                if (Data.GetType() == type)
                {
                    if (_column.Count < nColumn)
                    {
                        while (_column.Count == (nColumn-1))
                        {
                            _column.Add(null);
                        }
                    }
                    _column.Add(Data);
                    _result = true;
                }
            }
            return _result;
        }

        bool WriteData(string FileName)
        {
            FileInfo fileInfo = new FileInfo(FileName);
            StreamWriter sWriter = fileInfo.CreateText();
            List<string> lines = new List<string>();

            foreach (string rowName in rowNameList)
            {
                int index = 0;
                lines[index] += rowName;
                lines[index] += ", ";
                List<object> _column = (List<object>)rowTable[rowName];
                foreach(object _data in _column)
                {
                    index++;
                    lines[index] += _data.ToString();
                    lines[index] += ", ";
                }
            }

            foreach (string _line in lines)
            {
                sWriter.WriteLine(_line);
            }

            sWriter.Close();
            
            bool _result = false;
            return _result;
        }
    }
}

